package cn.rengy.isql;

import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
 * 未命名参数源
 *
 */
public class UnNamedSqlParameterSource implements SqlParameterSource {

	private final List<Object> values = new ArrayList<Object>();
	
	public UnNamedSqlParameterSource(Object ... args) {
		this.values.addAll(Stream.of(args).collect(Collectors.toList()));
	}
	
	public UnNamedSqlParameterSource(List<Object> values) {
		this.values.addAll(values);
	}
	public UnNamedSqlParameterSource(SqlParameterSource sqlParameterSource) {
		if(sqlParameterSource instanceof UnNamedSqlParameterSource) {
			this.values.addAll(((UnNamedSqlParameterSource)sqlParameterSource).getValues());
		}else {
			String[] names=sqlParameterSource.getParameterNames();
			for(String name:names) {
				this.values.add(sqlParameterSource.getValue(name));
			}
		}
	}
	public UnNamedSqlParameterSource(Map<String, ?> values) {
		this.values.addAll(values.values());
	}
	public UnNamedSqlParameterSource addValue(Object[] args) {
		
		this.values.addAll(Stream.of(args).collect(Collectors.toList()));
		return this;
	}
	public UnNamedSqlParameterSource addValue(Object args) {
		
		this.values.add(args);
		return this;
	}
	public UnNamedSqlParameterSource addValue(Collection<?> args) {
		this.values.addAll(args);
		return this;
	}
	public List<Object> getValues() {
		return this.values;
	}
	@Override
	public boolean hasValue(String paramName) {
		throw new IllegalArgumentException("UnNamedSqlParameterSource不支持此方法");
	}
	
	public boolean hasValue(int i) {
		if(i>=values.size()){
			return false;
		}
		return true;
	}
	
	@Override
	public Object getValue(String paramName) throws IllegalArgumentException {
		throw new IllegalArgumentException("UnNamedSqlParameterSource不支持此方法");
	}

	public Object getValue(int i){
		if(i>=values.size()){
			return null;
		}
		return values.get(i);
	}
}
